package com.dayatang.android.Util;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;

public class BitmapUtil {
	
	public static Bitmap CreateMyTaskCountIcon(Bitmap icon, Context context, String taskNum){
		int iconSize = getIconSize(context);
		
		Bitmap contactIcon = createContactIcon(context);
        Canvas canvas = new Canvas(contactIcon);
        InitBitmapPaint(icon,context,canvas);
        
        Paint circlePaint = new Paint();
        circlePaint.setAntiAlias(true);
        circlePaint.setColor(Color.RED);
        canvas.drawCircle(iconSize-9,10, 10, circlePaint);
        
        Paint countPaint = getTextPaint("while");
        if(taskNum.equals("NoMyTask")){
        	canvas.drawText("0", iconSize-13, 14, countPaint);   
        }else{
        	Log.i("123", taskNum);
            canvas.drawText(taskNum, iconSize-13, 14, countPaint);   
        }
        
        return contactIcon;   
	}
	
	public static BitmapDrawable drawPersonName(Resources res, Bitmap icon, Context context, String name) {
		
		if (icon == null) {
			Log.i("123", "abc");
		}
		
		Bitmap contactIcon = createContactIcon(context);
        Canvas canvas = new Canvas(contactIcon);
        InitBitmapPaint(icon,context,canvas);
        
        Paint countPaint = getTextPaint("red");
        countPaint.setTextSize(22f);
        canvas.drawText(name,7,35,countPaint);   
		
		return new BitmapDrawable(res,contactIcon);
	}
	
	private static Bitmap createContactIcon(Context context) {
		int iconSize = getIconSize(context);
		return Bitmap.createBitmap(iconSize, iconSize, Config.ARGB_8888);   
	}
	
	private static int getIconSize(Context context) {
		Log.i("123", String.valueOf(context.getResources().getDimension(android.R.dimen.app_icon_size)));
        return (int)context.getResources().getDimension(android.R.dimen.app_icon_size);   
	}
	
	private static void InitBitmapPaint(Bitmap icon, Context context, Canvas canvas) {
		int iconSize = getIconSize(context);
        Paint iconPaint=new Paint();   
        iconPaint.setDither(true);
        iconPaint.setFilterBitmap(true);
        //Rect src=new Rect(0, 0, icon.getWidth(), icon.getHeight());   
        Rect dst=new Rect(0, 0, iconSize, iconSize);   
        canvas.drawBitmap(icon, null, dst, iconPaint);   
	}
	
	private static Paint getTextPaint(String textColor) {
        Paint countPaint=new Paint(Paint.ANTI_ALIAS_FLAG|Paint.DEV_KERN_TEXT_FLAG);   
        if (textColor.equals("while")) {
        	countPaint.setColor(Color.WHITE);   
        } else if (textColor.equals("red")) {
        	countPaint.setColor(Color.RED);   
        }
        countPaint.setTextSize(18f);   
        countPaint.setTypeface(Typeface.DEFAULT_BOLD);   
        return countPaint;
	}
	
}	
